package com.vim.modules.web.service;

import com.vim.common.base.CrudServiceImpl;
import com.vim.modules.web.dao.GenMapper;
import com.vim.modules.web.model.Column;
import com.vim.modules.web.model.Table;
import com.vim.modules.web.model.TableDTO;
import com.vim.modules.web.utils.DataType;
import com.vim.modules.web.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GenService extends CrudServiceImpl<Table>{

    @Resource
    private GenMapper genMapper;

    @Autowired
    public GenService(GenMapper mapper) {
        super(mapper);
    }

    public Table findTableColumnList(TableDTO dto) {
        Table table = new Table();
        /**表名*/
        table.setTableName(dto.getTableName());
        table.setClassName(StringUtil.buildClassName(dto.getTableName()));
        table.setBeanName(StringUtil.buildBeanName(dto.getTableName()));

        /**字段集合*/
        List<Column> list = genMapper.findTableColumnList(dto);
        DataType dataType = new DataType();
        for(Column temp:list){
            /**转换类型和驼峰命名*/
            temp.setDataType(dataType.getDataType(temp.getDataType()));
            temp.setFieldName(StringUtil.buildBeanName(temp.getColumnName()));

            if("PRI".equals(temp.getColumnKey())){
                table.setIdName(temp.getFieldName());
                table.setIdName4Upper(temp.getFieldName4Upper());
            }
        }

        table.setColumns(list);
        return table;
    }

    /**
     * 查询所有表
     */
    public List<String> findTableList(){
        return genMapper.findTableList();
    }
}
